package com.suyang.syt.actools.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import com.suyang.syt.actools.util.DztStringUtil;
import com.suyang.syt.actools.util.FileUtil;

public class ActTools {
	public static String author = "administrator";
	public static String description = " ";
	/* 模块名称 */
	// public static String model = "admin";
	public static String model = "";

	/* 实体名称（建议使用驼峰命名法定义） */
	public static String entityClass = "RoleResourcesPermission";
	public static String lowerClass = DztStringUtil.toLowerCaseFirstOne(entityClass);// "keyword";//
	// 实体类变量名
	public static String sqlAlias = DztStringUtil.toSqlAlias(entityClass);// 数据查询别名

	/* controller */
	public static String basepackage = "com.suyang.syt";
	public static String controllerpackage = basepackage + "." + "controller";
	public static String daopackage = basepackage + ".dao.interfaces";
	public static String daoimplpackage = basepackage + ".dao.impl";
	public static String entitypackage = basepackage + ".model";
	public static String entitydtopackage = basepackage + ".model.dto";
	public static String servicepackage = basepackage + ".service.interfaces";;
	public static String serviceimplpackage = basepackage + ".service.impl";;
	public static String jsppath = "webapp/WEB-INF/jsp";
	public static String jspath = "webapp/resources/js/jsp";

	public static void createControllerClass() throws IOException {
		String newClassname = getHomeDir("src/main/java") + "/" + controllerpackage.replace(".", "/") + "/" + entityClass + "Controller.java";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main/java/" + basepackage.replace(".", "/") + "/actools/template/controller") + "/" + "TemplateController.java");
		new File(newClassname).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newClassname)) {
			buildClass(templateContent, newClassname, controllerpackage);
			System.out.println("[模板构建][Controller]===>  " + newClassname + "[OK]");
		} else {
			System.out.println("[模板构建][Controller]===>  " + entityClass + "Controller.java 该类已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newClassname, controllerpackage);
				System.out.println("[模板构建][Controller]===>  " + newClassname + "[覆盖OK]");
			}
		}
	}

	public static void createEntityClass() throws IOException {
		String newClassname = getHomeDir("src/main/java") + "/" + entitypackage.replace(".", "/") + "/" + entityClass + ".java";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main/java/" + basepackage.replace(".", "/") + "/actools/template/model") + "/" + "Template.java");
		new File(newClassname).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newClassname)) {
			buildClass(templateContent, newClassname, entitypackage);
			System.out.println("[模板构建][Entity]===>  " + newClassname + "[OK]");
		} else {
			System.out.println("[模板构建][Entity]===>  " + entityClass + ".java 该类已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newClassname, entitypackage);
				System.out.println("[模板构建][Entity]===>  " + newClassname + "[覆盖OK]");
			}
		}

	}

	public static void createEntityDTOClass() throws IOException {
		String newClassname = getHomeDir("src/main/java") + "/" + entitydtopackage.replace(".", "/") + "/" + entityClass + "DTO.java";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main/java/" + basepackage.replace(".", "/") + "/actools/template/model/dto") + "/" + "TemplateDTO.java");
		new File(newClassname).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newClassname)) {
			buildClass(templateContent, newClassname, entitydtopackage);
			System.out.println("[模板构建][EntityDTO]===>  " + newClassname + "[OK]");
		} else {
			System.out.println("[模板构建][EntityDTO]===>  " + entityClass + "DTO.java 该类已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newClassname, entitydtopackage);
				System.out.println("[模板构建][EntityDTO]===>  " + newClassname + "[覆盖OK]");
			}
		}

	}

	public static void createServiceClass() throws IOException {
		String newClassname = getHomeDir("src/main/java") + "/" + servicepackage.replace(".", "/") + "/" + entityClass + "Service.java";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main/java/" + basepackage.replace(".", "/") + "/actools/template/service/interfaces") + "/" + "TemplateService.java");
		new File(newClassname).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newClassname)) {
			buildClass(templateContent, newClassname, servicepackage);
			System.out.println("[模板构建][Service]===>  " + newClassname + "[OK]");
		} else {
			System.out.println("[模板构建][Service]===>  " + entityClass + "Service.java 该类已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newClassname, servicepackage);
				System.out.println("[模板构建][Service]===>  " + newClassname + "[覆盖OK]");
			}
		}
	}

	public static void createServiceImplClass() throws IOException {
		String newClassname = getHomeDir("src/main/java") + "/" + serviceimplpackage.replace(".", "/") + "/" + entityClass + "ServiceImpl.java";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main/java/" + basepackage.replace(".", "/") + "/actools/template/service/impl") + "/" + "TemplateServiceImpl.java");
		new File(newClassname).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newClassname)) {
			buildClass(templateContent, newClassname, serviceimplpackage);
			System.out.println("[模板构建][ServiceImpl]===>  " + newClassname + "[OK]");
		} else {
			System.out.println("[模板构建][ServiceImpl]===>  " + entityClass + "ServiceImpl.java 该类已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newClassname, serviceimplpackage);
				System.out.println("[模板构建][ServiceImpl]===>  " + newClassname + "[覆盖OK]");
			}
		}
	}

	public static void createDaoClass() throws IOException {
		String newClassname = getHomeDir("src/main/java") + "/" + daopackage.replace(".", "/") + "/" + entityClass + "Dao.java";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main/java/" + basepackage.replace(".", "/") + "/actools/template/dao/interfaces") + "/" + "TemplateDao.java");
		new File(newClassname).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newClassname)) {
			buildClass(templateContent, newClassname, daopackage);
			System.out.println("[模板构建][Dao]===>  " + newClassname + "[OK]");
		} else {
			System.out.println("[模板构建][Dao]===>  " + entityClass + "Dao.java 该类已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newClassname, daopackage);
				System.out.println("[模板构建][Dao]===>  " + newClassname + "[覆盖OK]");
			}
		}
	}

	public static void createDaoImplClass() throws IOException {
		String newClassname = getHomeDir("src/main/java") + "/" + daoimplpackage.replace(".", "/") + "/" + entityClass + "DaoImpl.java";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main/java/" + basepackage.replace(".", "/") + "/actools/template/dao/impl") + "/" + "TemplateDaoImpl.java");
		new File(newClassname).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newClassname)) {
			buildClass(templateContent, newClassname, daoimplpackage);
			System.out.println("[模板构建][DaoImpl]===>  " + newClassname + "[OK]");
		} else {
			System.out.println("[模板构建][DaoImpl]===>  " + entityClass + "DaoImpl.java 该类已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newClassname, daoimplpackage);
				System.out.println("[模板构建][DaoImpl]===>  " + newClassname + "[覆盖OK]");
			}
		}
	}

	public static void createHTML() throws IOException {
		String newFilename = getHomeDir("src/main") + "/" + jsppath + "/" + lowerClass + "/list.jsp";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main") + "/" + jsppath + "/" + "/template/list.jsp");
		new File(newFilename).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newFilename)) {
			buildClass(templateContent, newFilename, jsppath);
			System.out.println("[模板构建][JSP]===>  " + newFilename + "[OK]");
		} else {
			System.out.println("[模板构建][JSP]===>  " + entityClass + ".jsp 已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newFilename, jsppath);
				System.out.println("[模板构建][JSP]===>  " + newFilename + "[覆盖OK]");
			}
		}
	}

	public static void createJS() throws IOException {
		String newFilename = getHomeDir("src/main") + "/" + jspath + "/" + lowerClass + "/list.js";
		String templateContent = FileUtil.getFileContent(getHomeDir("src/main") + "/" + jspath + "/template/" + "list.js");
		new File(newFilename).getParentFile().mkdirs();
		if (!FileUtil.isFileExist(newFilename)) {
			buildClass(templateContent, newFilename, jspath);
			System.out.println("[模板构建][JS]===>  " + newFilename + "[OK]");
		} else {
			System.out.println("[模板构建][JS]===>  " + entityClass + ".JS 已存在,是否覆盖?y/n");
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = reader.readLine();
			if (line != null && line.equalsIgnoreCase("y")) {
				buildClass(templateContent, newFilename, jspath);
				System.out.println("[模板构建][JS]===>  " + newFilename + "[覆盖OK]");
			}
		}
	}

	private static String getHomeDir(String dir) throws IOException {
		return System.getProperty("user.dir") + "\\" + dir.replace("/", "\\");
	}

	private static void buildClass(String templateContent, String filename, String packagename) throws IOException {
		entityClass = filename.replace(getHomeDir("src/main/java") + "/" + packagename.replace(".", "/") + "/", "").replace("Dao.java", "").replace("DaoImpl.java", "").replace("Service.java", "").replace("ServiceImpl.java", "").replace("Controller.java", "").replace("DTO.java", "")
				.replace(".java", "");
		FileWriter fw = new FileWriter(filename);
		fw.write(replaceTemplate(templateContent));
		fw.flush();
		fw.close();
	}

	@SuppressWarnings("deprecation")
	private static String replaceTemplate(String templateContent) {
		return templateContent.replace("actools.template.", "").replace("tme_author", author).replace("tme_date", new Date().toLocaleString()).replace("tme_description", description).replace("Template", entityClass).replace("template", lowerClass).replace("sqlAlias", sqlAlias);
	}

	public static void main(String[] args) throws IOException {

		createEntityClass();
		createEntityDTOClass();
		createDaoClass();
		createDaoImplClass();
		createServiceClass();
		createServiceImplClass();
		createControllerClass();
		createHTML();
		createJS();

		// createControllerClass();
	}
}
